<?php

namespace app\admin\model\litestore;
use think\Model;
use think\Db;
use think\Log;
use think\Exception;
use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\pifa\ActivitySku;
class Litestoregoodsspec extends Model
{

    // 表名
    protected $name = 'litestore_goods_spec';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * 批量添加商品sku记录
     * @param $goods_id
     * @param $spec_list
     * @return array|false
     * @throws \Exception
     */
    public function addSkuList($goods_id, $spec_list)
    {   


        Db::startTrans();
        try {
            Db::table('fa_litestore_goods_spec')->where('goods_id','=', $goods_id)->delete();
            $data = [];

            foreach ($spec_list as $key => $item) {
                $now_data = [];
                $spec_attr = '';
                $spec_sku_id = explode('_',$item['spec_sku_id']);

                $map['id'] = ['in',$spec_sku_id];
                $spec_attr_item = Db::table('fa_litestore_spec_value')->where($map)->column('spec_value');

                $spec_attr = implode('/', $spec_attr_item);


        

                $data[$key] = array_merge($item['form'], [
                    'spec_sku_id' => $item['spec_sku_id'],
                    'spec_name' => $spec_attr,
                    'goods_id' => $goods_id,
                ]);


                if (isset($data[$key]['daoqi']) && empty($data[$key]['daoqi'])) {
                    unset($data[$key]['daoqi']);
                }


                $this->insert($data[$key],true);     
            }
            Db::commit();

        } catch (\Exception $e) {
            Db::rollback();
            Log::error($e->getMessage());
            throw new Exception($e->getMessage());
        }


        

        
        
        
        // logs('goods_save',json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        
        
        // $uniformData = array_map(function($item) use ($data) {
        //     $newItem = [];
        //     foreach ($data as $field) {
        //         $newItem[$field] = isset($item[$field]) ? $item[$field] : null;
        //     }
        //     return $newItem;
        // }, $originalData);
       
    
        // $this->insertAll($data,true);


        // $map['goods_id'] = $goods_id;

        // $list = Db::table('fa_litestore_goods_spec')->where($map)->select();

        // foreach ($list as $key => $value) {

        //     if (!empty($value['spec_sku_id'])) {
        //         $Wxlitestoregoods = new Wxlitestoregoods();
        //         $goods = $Wxlitestoregoods->detail($goods_id);
        //         $specAttrData = $goods->getGoodsSku($value['spec_sku_id']);
        //         $specAttrData = json_decode(json_encode($specAttrData),true);
        //         $map2['goods_spec_id'] = $value['goods_spec_id'];
        //         $updata['miaoshu'] = $specAttrData['goods_attr'];

        //         Db::table('fa_litestore_goods_spec')->where($map2)->update($updata);

        //     }
                
        // }
        return;
    }

    /**
     * 添加商品规格关系记录
     * @param $goods_id
     * @param $spec_attr
     * @return array|false
     * @throws \Exception
     */
    public function addGoodsSpecRel($goods_id, $spec_attr)
    {
        $data = [];
        array_map(function ($val) use (&$data, $goods_id) {
            array_map(function ($item) use (&$val, &$data, $goods_id) {
                $data[] = [
                    'goods_id' => $goods_id,
                    'spec_id' => $val['group_id'],
                    'spec_value_id' => $item['item_id'],
                ];
            }, $val['spec_items']);
        }, $spec_attr);
        $model = new Litestoregoodsspecrel;
        $model->where('goods_id','=', $goods_id)->delete();

        return $model->saveAll($data);
    }

    /**
     * 移除指定商品的所有sku
     * @param $goods_id
     * @return int
     */
    public function removeAll($goods_id)
    {
        $model = new Litestoregoodsspecrel;
        $model->where('goods_id','=', $goods_id)->delete();
        return $this->where('goods_id','=', $goods_id)->delete();
    }
    public function activitySkuPrice() 
    {
        return $this->hasOne(ActivitySku::class, 'goods_sku_price_id', 'goods_spec_id');
    }
     public function goods() 
    {
        return $this->hasOne(Litestoregoods::class, 'goods_id', 'goods_id');
    }

 


     public function good()
    {
        return $this->belongsTo('app\admin\model\litestore\Litestoregoods', 'goods_id', 'goods_id', [], 'LEFT')->setEagerlyType(0);
    }
}
